using System;
using System.Web.UI.WebControls;
using TrainGame.Model;
using TrainGame.GlobalState;

namespace _1856.UI
{
	/// <summary>
	/// Summary description for BuyNewTrain.
	/// </summary>
	public partial class BuyOpenMarketTrain : System.Web.UI.Page
	{
	
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ID = "BuyOpenMarketTrain";

		}
		#endregion

		protected void Page_Load(object sender, System.EventArgs e)
		{
			ErrorMsg.Text = "";
			
			FillTableWithTrainInfo();
		}


		private void FillTableWithTrainInfo()
		{
			Game	game = Global.getGame (this);;

			int				nQuantity2		= 0;
			int				nQuantity3		= 0;
			int				nQuantity4		= 0;
			int				nQuantity5		= 0;
			int				nQuantity6		= 0;
			Button			button;
			TableCell		tableCell;
			PublicCompany	company;

			company = game.CurrentlyOperatingCompany;
			CompanyMoney.Text = company.Name + " has $" + company.Cash;
				
			foreach (Train train in game.Trains_OpenMarket)
			{
				if (train.Name == "2")
					nQuantity2++;
				
				if (train.Name == "3")
					nQuantity3++;
				
				if (train.Name == "4")
					nQuantity4++;
				
				if (train.Name == "5")
					nQuantity5++;
				
				if (train.Name == "6")
					nQuantity6++;
			}

			// set info for 2 train
			if (nQuantity2 > 0)
			{
				tableCell		= (TableCell) TrainTable.FindControl("Quanity_2"); 
				tableCell.Text	= nQuantity2.ToString();
			
				tableCell		= (TableCell) TrainTable.FindControl("Buy_2"); 
				button			= new Button();
				button.Text		= "Buy";
				button.Click	+= new System.EventHandler(this.BuyTrain);
				button.CommandArgument = "2";
				tableCell.Controls.Add(button);
			}
			else
			{
				TrainTable.Rows[1].Visible = false;
			}
			
			// set info for 3 train
			if (nQuantity3 > 0)
			{
				tableCell		= (TableCell) TrainTable.FindControl("Quanity_3"); 
				tableCell.Text	= nQuantity3.ToString();
			
				tableCell		= (TableCell) TrainTable.FindControl("Buy_3"); 
				button			= new Button();
				button.Text		= "Buy";
				button.Click	+= new System.EventHandler(this.BuyTrain);
				button.CommandArgument = "3";
				tableCell.Controls.Add(button);
			}
			else
			{
				TrainTable.Rows[2].Visible = false;
			}
			
			// set info for 4 train
			if (nQuantity4 > 0)
			{
				tableCell		= (TableCell) TrainTable.FindControl("Quanity_4"); 
				tableCell.Text	= nQuantity4.ToString();
			
				tableCell		= (TableCell) TrainTable.FindControl("Buy_4"); 
				button			= new Button();
				button.Text		= "Buy";
				button.Click	+= new System.EventHandler(this.BuyTrain);
				button.CommandArgument = "4";
				tableCell.Controls.Add(button);
			}
			else
			{
				TrainTable.Rows[3].Visible = false;
			}
			
			// set info for 5 train
			if (nQuantity5 > 0)
			{
				tableCell		= (TableCell) TrainTable.FindControl("Quanity_5"); 
				tableCell.Text	= nQuantity5.ToString();
			
				tableCell		= (TableCell) TrainTable.FindControl("Buy_5"); 
				button			= new Button();
				button.Text		= "Buy";
				button.Click	+= new System.EventHandler(this.BuyTrain);
				button.CommandArgument = "5";
				tableCell.Controls.Add(button);
			}
			else
			{
				TrainTable.Rows[4].Visible = false;
			}
			
			// set info for 6 train
			if (nQuantity6 > 0)
			{
				tableCell		= (TableCell) TrainTable.FindControl("Quanity_6"); 
				tableCell.Text	= nQuantity6.ToString();
			
				tableCell		= (TableCell) TrainTable.FindControl("Buy_6"); 
				button			= new Button();
				button.Text		= "Buy";
				button.Click	+= new System.EventHandler(this.BuyTrain);
				button.CommandArgument = "6";
				tableCell.Controls.Add(button);
			}
			else
			{
				TrainTable.Rows[5].Visible = false;
			}
			
			// do only 8 or D but not both and hide the non-appropriate row and column
			if (game.MaxTrain8Train)
			{
				// hide the cells and row for the D train
				for (int i = 0; i < TrainTable.Rows.Count; i++)
					TrainTable.Rows[i].Cells[5].Visible = false;
			}
			else
			{
				// hide the cells and row for the 8 train
				for (int i = 0; i < TrainTable.Rows.Count; i++)
					TrainTable.Rows[i].Cells[4].Visible = false;
			}
			
			// hide 8 and D rows
			TrainTable.Rows[6].Visible = false;
			TrainTable.Rows[7].Visible = false;
		}
		

		private void BuyTrain(object sender, System.EventArgs e)
		{
			Game			game	= Global.getGame (this);;
			string			sTrain;
			Button			button	= (Button) sender;
			PublicCompany	company;

			company = game.CurrentlyOperatingCompany;
			sTrain	= button.CommandArgument;

			if (company != null)
			{
				string sErrorMsg;

				if (company.BuyOpenMarketTrain(sTrain, out sErrorMsg))
				{
					string sMsg;
					
					sMsg = company.Name + " bought a " + sTrain + " train";
					game.History.Add (sMsg);
					
					Response.Redirect("BuyOpenMarketTrain.aspx");
				}
				else
				{
					ErrorMsg.Text = sErrorMsg;
				}
			}
		}

		protected void Back_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("BuyTrain.aspx");
		}

	}
}
